package com.shinowit.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shinowit.dao.OrgInfoDao;
import com.shinowit.dao.mapper.SysOrgInfoMapper;
import com.shinowit.model.Result;
import com.shinowit.model.SysOrgInfo;
import com.shinowit.model.SysOrgInfoCriteria;
import com.shinowit.model.TreeNode;

@Controller
@RequestMapping("/system/org_info")
public class OrgInfoController {

	@Resource
	private OrgInfoDao org_info_dao;
	
	@Resource
	private SysOrgInfoMapper org_info_mapper_dao;	
	
	@RequestMapping(value="/orgtree")
	@ResponseBody
	public List<TreeNode> getAllOrgInfoTree(){
		return org_info_dao.getOrgInfoTree().children;
	}

	@RequestMapping(value={"","/"},method=RequestMethod.GET)
	public String defaultPage(){
		return "/org_info/list";
	}
	
	@RequestMapping(value="/page")
	@ResponseBody
	public Map<String,Object> getPage(@RequestParam(value="page",defaultValue="1") Integer pageIndex,@RequestParam(value="rows",defaultValue="10") Integer pageSize,SysOrgInfo org_info,String sort,String order){
		Map<String,Object> result=new HashMap<String,Object>();
		
		SysOrgInfoCriteria ex=new SysOrgInfoCriteria();
		SysOrgInfoCriteria.Criteria cr1= ex.createCriteria();
		
		if ((null!=org_info.getOrgCode()) && (org_info.getOrgCode().trim().length()>0)){
			cr1.andOrgCodeEqualTo(org_info.getOrgCode());
		}else{
			cr1.andOrgCodeEqualTo("01");//默认顶级节点数据值
		}

		String org_name=org_info.getOrgName();
		if ((null!=org_name) && (org_name.trim().length()>0)) {
			cr1.andOrgNameLike(org_name+"%");
		}
		
		ex.setPageIndex(pageIndex);
		ex.setPageSize(pageSize);
		
		if ((null!=sort) && (sort.trim().length()>0)){
			ex.setOrderByClause(sort);
		}
		
		if ((null!=order) && (order.trim().length()>0)){
			if (!"asc".equalsIgnoreCase(order)){
				ex.setOrderByClause(ex.getOrderByClause()+" desc");
			}
		}
		
		List<SysOrgInfo> data_list=org_info_dao.getOrgInfoTreeByExample(ex);
		
		result.put("rows", data_list);
		
		long record_count=org_info_mapper_dao.countByExample(ex);
		result.put("total", record_count);
		
		return result;
	}	
	

	@RequestMapping(value="/create")
	@ResponseBody
	public Map<String,Object> create_new(SysOrgInfo data){
		Map<String,Object> result_map=new HashMap<String,Object>();
		int i=org_info_mapper_dao.insert(data);
		if (1==i){
			result_map.put("msg", "保存成功!");
			result_map.put("status", true);
		}else{
			result_map.put("msg", "保存失败!");
			result_map.put("status", false);
		}
		return result_map;
	}

	@RequestMapping(value="/edit")
	@ResponseBody
	public Result edit_data(SysOrgInfo data){
		Result result=new Result();
		int i=org_info_mapper_dao.updateByPrimaryKey(data);
		if (1==i){
			result.setMsg("修改成功！");
			result.setStatus(true);
		}else{
			result.setMsg("修改失败！");
			result.setStatus(false);
		}
		return result;
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public Result delete_data(String orgCode){
		Result result=new Result();
		int i=org_info_mapper_dao.deleteByPrimaryKey(orgCode);
		if (1==i){
			result.setMsg("删除成功！");
			result.setStatus(true);
		}else{
			result.setMsg("删除失败！");
			result.setStatus(false);
		}
		return result;
	}		
	
}
